﻿using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Tools.Core.Mvvm;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using Tools.Core.Extensions;
using System.IO;
using Tools.Core.Dtos;

namespace Tools.Modules.Navigations.ViewModels
{
    public class PDFOCRViewModel : BindableBase,INavigationAware
    {
        private string _result;
        public string Result
        {
            get => _result;
            set=>SetProperty(ref _result, value);
        }
        private string _currentPdfPath;
        public string CurrentPdfPath
        {
            get => _currentPdfPath;
            set=>SetProperty(ref _currentPdfPath, value);
        }
        private ObservableCollection<PageOCRPlanDto> _pages=new ObservableCollection<PageOCRPlanDto>();
        public ObservableCollection<PageOCRPlanDto> Pages
        {
            get => _pages;
            set=>SetProperty(ref _pages, value);
        }

        public DelegateCommand ChoosePdfCommand { get; private set; }
        public DelegateCommand AllCheckCommand { get; private set; }
        public DelegateCommand AllUncheckCommand { get; private set; }
        public DelegateCommand ReverseCheckCommand { get; private set; }
        public DelegateCommand SaveResultCommand { get; private set; }
        public DelegateCommand RecognizeCommand { get; private set; }
        public PDFOCRViewModel()
        {
            ChoosePdfCommand = new DelegateCommand(() =>
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "PDF文档|*.pdf";
                if (ofd.ShowDialog() == true)
                {
                    CurrentPdfPath= ofd.FileName;
                    PdfDocument pdf=PdfReader.Open(CurrentPdfPath);
                    Pages = Enumerable.Range(0, pdf.Pages.Count).Select(i => new PageOCRPlanDto
                    {
                    }).AsObservableCollection();
                }
            });
            AllCheckCommand = new DelegateCommand(() =>
            {
                
            });
            AllUncheckCommand = new DelegateCommand(() =>
            {
                
            });
            ReverseCheckCommand = new DelegateCommand(() =>
            {
                
            });
            SaveResultCommand = new DelegateCommand(() =>
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.DefaultExt = ".txt";
                if (sfd.ShowDialog() == true)
                {
                    File.WriteAllText(sfd.FileName,Result);
                }
            });
            RecognizeCommand = new DelegateCommand(() =>
            {
                //TODO
            });
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
        }
    }
}
